package com.maxd.service.helpTask;


import com.maxd.model.*;
import com.maxd.respository.helpTask.*;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.service.message.IMessageService;
import com.maxd.service.money.IMoneyDetailsService;
import com.maxd.service.userMoney.UserMoneyDetailsService;
import com.maxd.service.userinfo.UserServiceImpl;
import com.maxd.utils.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 维权信息
 */
@Service
public class HelpMaintainServiceImpl implements HelpMaintainService {

    /** 维权信息 */
    @Autowired
    private HelpMaintainJpaRepository helpMaintainJpaRepository;
    /** 接单信息 */
    @Autowired
    private HelpSendOrderService helpSendOrderService;
    /** 维权详细信息 */
    @Autowired
    private HelpMaintainDetailsJpaRepository helpMaintainDetailsJpaRepository;
    /** 维权详细信息 */
    @Autowired
    private HelpMaintainModelRepository helpMaintainModelRepository;
    /** 维权自定义方法 */
    @Autowired
    private HelpMaintainRepository HelpMaintainRepository;
    /** 接单信息自定义方法类 */
    @Autowired
    private HelpSendOrderRepository helpSendOrderRepository;
    /** 助力信息自定义方法类 */
    @Autowired
    private HelpTaskRepository helpTaskRepository;
    /** 用户信息 */
    @Autowired
    private UserRepository userRepository;
    /** 用户钱包明细 */
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    /** 信誉分 */
    @Autowired
    private HelpUserScoreService helpUserScoreService;
    /** 信誉明细分 */
    @Autowired
    private HelpUserScoreDetailsService helpUserScoreDetailsService;
    /** 信誉明细分 */
    @Autowired
    private HelpTakeOrderService helpTakeOrderService;
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private IMoneyDetailsService moneyDetailsService;

    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    @Transactional
    public int insert(HelpMaintain helpMaintain,HelpSendOrder helpSendOrder) {
        helpMaintain.setCreateTime(sdf.format(new Date()));
        helpMaintain.setState(0);
        helpMaintain=helpMaintainJpaRepository.save(helpMaintain);
        HelpMaintainDetails oldHelpMaintainDetails=new HelpMaintainDetails();
        oldHelpMaintainDetails.setHelpMaintainId(helpMaintain.getId());
        oldHelpMaintainDetails.setClassify(1);
        oldHelpMaintainDetails.setContent(helpSendOrder.getAuditContent());
//        oldHelpMaintainDetails.setPicture(helpSendOrder.getPicture());
        oldHelpMaintainDetails.setCreateTime(helpSendOrder.getAuditTime());
        oldHelpMaintainDetails.setUserId(helpMaintain.getTakeOrderUserId());
        helpMaintainDetailsJpaRepository.save(oldHelpMaintainDetails);
        HelpMaintainDetails helpMaintainDetails=new HelpMaintainDetails();
        helpMaintainDetails.setHelpMaintainId(helpMaintain.getId());
        helpMaintainDetails.setClassify(2);
        helpMaintainDetails.setContent(helpSendOrder.getContent());
//        helpMaintainDetails.setPicture(helpSendOrder);
        helpMaintainDetails.setCreateTime(sdf.format(new Date()));
        helpMaintainDetails.setUserId(helpMaintain.getTakeOrderUserId());
        helpMaintainDetailsJpaRepository.save(helpMaintainDetails);
        helpSendOrderService.update(helpSendOrder.getId(),4);//接单状态修改为维权
        return 1;
    }

    @Override
    public Integer auditHelpMaintain(Long helpMaintainId,Integer state,String content){
        state=state-1;
        String date=sdf.format(new Date());
        HelpMaintain oldHelpMaintain = HelpMaintainRepository.selectById(helpMaintainId);
        if(oldHelpMaintain.getState()==0){
            HelpSendOrder helpSendOrder=helpSendOrderRepository.selectById(oldHelpMaintain.getHelpSendOrderId());
            HelpTakeOrder helpTakeOrder=helpTakeOrderService.selectById(oldHelpMaintain.getHelpTakeOrderId());
            UserInfo userSendOrder = userRepository.getUserById(helpSendOrder.getUserId());
            UserInfo userTakeOrder = userRepository.getUserById(helpTakeOrder.getUserId());
            MessageInfo messageInfo = new MessageInfo();
            MessageInfo messageInfo1 = new MessageInfo();
            if(state==1){//接单人获胜
                //先获取之前的接单拒绝原因，并将扣除的费用加回来
                HelpTask helpTask= helpTaskRepository.selectById(helpSendOrder.getHelpTaskId());
                if(helpSendOrder.getCategory()==1){
                    helpUserScoreService.updateUserScore(helpSendOrder.getUserId(),1,2);
                    HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
                    helpUserScoreDetails.setUserId(helpSendOrder.getUserId());
                    helpUserScoreDetails.setTitle("[维权获胜]任务Id："+helpSendOrder.getHelpTaskId());
                    helpUserScoreDetails.setContent("增加积分：2");
                    helpUserScoreDetails.setCreateTime(date);
                    helpUserScoreDetails.setScore(2);
                    helpUserScoreDetails.setType(1);
                    helpUserScoreDetailsService.save(helpUserScoreDetails);
                }else if(helpSendOrder.getCategory()==2){
                    helpUserScoreService.updateUserScore(helpSendOrder.getUserId(),1,3);
                    HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
                    helpUserScoreDetails.setUserId(helpSendOrder.getUserId());
                    helpUserScoreDetails.setTitle("[维权获胜]任务Id："+helpSendOrder.getHelpTaskId());
                    helpUserScoreDetails.setContent("增加积分：3");
                    helpUserScoreDetails.setCreateTime(date);
                    helpUserScoreDetails.setScore(3);
                    helpUserScoreDetails.setType(1);
                    helpUserScoreDetailsService.save(helpUserScoreDetails);
                }
                //修改接单状态
                helpSendOrderRepository.updateHelpSendOrder(oldHelpMaintain.getHelpSendOrderId(),2,"已同意",0,sdf.format(new Date()));
                //给接单人增加金额
                DecimalFormat formater = new DecimalFormat();
                formater.setMaximumFractionDigits(2);
                formater.setGroupingSize(0);
                formater.setRoundingMode(RoundingMode.FLOOR);
                Double money=Double.parseDouble(formater.format(helpTask.getTaskPrice()));
                userRepository.updateJiFenAdd(money.toString(),helpSendOrder.getUserId());
                MoneyDetails userMoneyDetails=new MoneyDetails();
                userMoneyDetails.setUserId(helpSendOrder.getUserId());
                userMoneyDetails.setTitle("[完成任务]任务ID："+helpTask.getId());
                userMoneyDetails.setContent("增加金额:"+money);
                userMoneyDetails.setType(1);
                userMoneyDetails.setMoney(money);
                userMoneyDetails.setCreateTime(date);
                moneyDetailsService.save(userMoneyDetails);
                //完成接单增加信誉分
                helpUserScoreService.updateUserScore(helpSendOrder.getUserId(),1,1);
                HelpUserScoreDetails helpUserScoreDetails=new HelpUserScoreDetails();
                helpUserScoreDetails.setUserId(helpSendOrder.getUserId());
                helpUserScoreDetails.setTitle("[完成任务]任务Id："+helpSendOrder.getHelpTaskId());
                helpUserScoreDetails.setContent("增加积分：1");
                helpUserScoreDetails.setCreateTime(date);
                helpUserScoreDetails.setScore(1);
                helpUserScoreDetails.setType(1);
                helpUserScoreDetailsService.save(helpUserScoreDetails);

                //判定派单人失败 扣除信用分
                helpUserScoreService.updateUserScore(helpTakeOrder.getUserId(),2,1);
                HelpUserScoreDetails helpUserScoreDetails1=new HelpUserScoreDetails();
                helpUserScoreDetails1.setUserId(helpTakeOrder.getUserId());
                helpUserScoreDetails1.setTitle("[维权失败]任务Id："+helpSendOrder.getHelpTaskId());
                helpUserScoreDetails1.setContent("扣除积分：1");
                helpUserScoreDetails1.setCreateTime(date);
                helpUserScoreDetails1.setScore(1);
                helpUserScoreDetails1.setType(2);
                helpUserScoreDetailsService.save(helpUserScoreDetails);



                messageInfo.setContent(userSendOrder.getNickName() + " 您好！您的维权判定成功！");
                if (userSendOrder.getClientid() != null) {
                    userService.pushToSingle("任务维权", userSendOrder.getNickName() + " 您好！您的维权判定成功！", userSendOrder.getClientid());
                }
                messageInfo1.setContent(userTakeOrder.getNickName() + " 您好！您的维权判定失败！");
                if (userTakeOrder.getClientid() != null) {
                    userService.pushToSingle("任务维权", userTakeOrder.getNickName() + " 您好！您的维权判定失败！", userTakeOrder.getClientid());
                }
            }else{
                messageInfo.setContent(userSendOrder.getNickName() + " 您好！您的维权判定失败！");
                if (userSendOrder.getClientid() != null) {
                    userService.pushToSingle("任务维权", userSendOrder.getNickName() + " 您好！您的维权判定失败！", userSendOrder.getClientid());
                }
                messageInfo1.setContent(userTakeOrder.getNickName() + " 您好！您的维权判定成功！");
                if (userTakeOrder.getClientid() != null) {
                    userService.pushToSingle("任务维权", userTakeOrder.getNickName() + " 您好！您的维权判定成功！", userTakeOrder.getClientid());
                }
                //修改接单状态
                helpSendOrderRepository.updateHelpSendOrder(oldHelpMaintain.getHelpSendOrderId(),6,"维权失败",0,sdf.format(new Date()));
            }
            messageInfo.setState(String.valueOf(6));
            messageInfo.setTitle("任务维权");
            messageInfo.setUserName(userSendOrder.getNickName());
            messageInfo.setUserId(String.valueOf(userSendOrder.getId()));
            iMessageService.saveBody(messageInfo);
            messageInfo1.setState(String.valueOf(6));
            messageInfo1.setTitle("任务维权");
            messageInfo1.setUserName(userSendOrder.getNickName());
            messageInfo1.setUserId(String.valueOf(userSendOrder.getId()));
            iMessageService.saveBody(messageInfo1);
            return HelpMaintainRepository.updateState(helpMaintainId,state,content);
        }
        return 1;
    }

    @Override
    public Page<HelpMaintainModel> selectHelpMaintainList(Pageable pageable) {
        return helpMaintainModelRepository.selectHelpMaintainList(pageable);
    }


    @Override
    public Page<Map<String, Object>> selectHelpMaintainListBySendOrder(Long sendOrderUserId,Integer state, Pageable pageable) {
        if(state==null ||state==0){
            return HelpMaintainRepository.selectHelpMaintainListBySendOrder(sendOrderUserId,pageable);
        }
        return HelpMaintainRepository.selectHelpMaintainListBySendOrders(sendOrderUserId,pageable);
    }

    @Override
    public Page<Map<String, Object>> selectHelpMaintainListByTakeOrder(Long takeOrderUserId,Integer state, Pageable pageable) {
        if(state==null ||state==0){
            return HelpMaintainRepository.selectHelpMaintainListByTakeOrder(takeOrderUserId,pageable);
        }
        return HelpMaintainRepository.selectHelpMaintainListByTakeOrders(takeOrderUserId,pageable);
    }

    @Override
    public HelpMaintain selectById(Long helpMaintainId) {
        return HelpMaintainRepository.selectById(helpMaintainId);
    }

    @Override
    public Integer selectCount(Long helpTaskId,Integer state) {
        return HelpMaintainRepository.selectCount(helpTaskId,state);
    }
}